package mop.util.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * This filter will filter a list on it's first [max] entries.
 * 
 * @author MOP Team 7
 *
 * @param <T>
 */
public class MaxCutOffFilter<T> implements Filter<T>{
	/**
	 * Constructor
	 * @param max	Maximum cutoff value
	 * @effect Max is set
	 * 	|setMax(max)
	 */
	public MaxCutOffFilter(int max){
		setMax(max);
	}
	
	/**
	 * The amount of entries we want to keep
	 */
	private int max = 0;
	
	/**
	 * Set max cutoff value
	 * @param max
	 * @post Max is set to given value
	 * 	|getMax() == max
	 */
	public void setMax(int max){
		if(max < 0)
			throw new IllegalArgumentException("Maximum has to be >= 0 in MaxCutOff filter.");
		
		this.max = max;
	}
	
	/**
	 * Return max cutoff value
	 * @return this.max
	 */
	public int getMax(){
		return this.max;
	}
	
	/**
	 * @see Filter#filter(List);
	 * @throws IllegalArgumentException
	 * 			If max is less then 0
	 */
	@Override
	public List<T> filter(List<T> list) {
		
		List<T> newList = new ArrayList<T>(max);
		newList.addAll(list);
		
		return newList.subList(0, Math.min(newList.size(), this.getMax()));

	}

}
